{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 感知机是什么？\n",
    ">- 答:感知机又可以被称为人工神经元，感知机能够接受输入多个信号,输出一个信息号.注意,感知机的信号只有0和1两种取值。感知机的输出取决于阈值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.3感知机的实现\n",
    "### 2.3.1.简单的实现"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0\n",
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "def AND(x1,x2): # 实现 \"与\" 门\n",
    "    w1,w2,theta = 0.5,0.5,0.7\n",
    "    tmp = w1*x1+w2*x2\n",
    "    if tmp <= theta:\n",
    "        return 0\n",
    "    elif tmp >theta:\n",
    "        return 1\n",
    "\n",
    "print(AND(0,0))\n",
    "print(AND(1,0))\n",
    "print(AND(0,1))\n",
    "print(AND(1,1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-26T08:46:40.609571200Z",
     "start_time": "2023-08-26T08:46:40.597574300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.  0.5]\n"
     ]
    }
   ],
   "source": [
    "# 导入权重和偏置\n",
    "import numpy as np\n",
    "x = np.array([0,1]) # 输入信号\n",
    "w = np.array([0.5,0.5]) # 权重\n",
    "b =-0.7 # 偏置\n",
    "print(w*x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-26T09:26:11.614487700Z",
     "start_time": "2023-08-26T09:26:10.357495200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "0.5"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(w*x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-26T09:26:25.820493300Z",
     "start_time": "2023-08-26T09:26:25.808501400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "-0.19999999999999996"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(w*x)+b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-26T09:26:42.862069300Z",
     "start_time": "2023-08-26T09:26:42.836079600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 实现与非门\n",
    "def NAND(x1,x2):\n",
    "    x = np.array([x1,x2])\n",
    "    w = np.array([-0.5,-0.5])\n",
    "    b = 0.7\n",
    "    tmp = np.sum(w*x)+b\n",
    "    if tmp<=0:\n",
    "        return 0\n",
    "    elif tmp >0:\n",
    "        return 1\n",
    "\n",
    "# 实现与门\n",
    "def AND(x1,x2):\n",
    "    x = np.array([x1,x2])\n",
    "    w = np.array([0.5,0.5])\n",
    "    b = -0.7\n",
    "    tmp = np.sum(w*x)+b\n",
    "    if tmp <=0:\n",
    "        return 0\n",
    "    elif tmp >0:\n",
    "        return 1\n",
    "\n",
    "# 实现或门\n",
    "def OR(x1,x2):\n",
    "    x = np.array([x1,x2])\n",
    "    w = np.array([0.5,0.5])\n",
    "    b = -0.2\n",
    "    tmp = np.sum(w*x) + b\n",
    "    if tmp <=0:\n",
    "        return 0\n",
    "    elif tmp>0:\n",
    "        return 1\n",
    "\n",
    "OR(1,0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-27T04:36:35.375753800Z",
     "start_time": "2023-08-27T04:36:35.193790400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# 定义异或门\n",
    "def NOR(x1,x2):\n",
    "    s1 = OR(x1,x2)\n",
    "    s2 = NAND(x1,x2)\n",
    "    y = AND(s1,s2)\n",
    "    return y\n",
    "\n",
    "print(NOR(1,1))\n",
    "print(NOR(1,0))\n",
    "print(NOR(0,1))\n",
    "print(NOR(0,0))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-27T04:50:54.750802600Z",
     "start_time": "2023-08-27T04:50:54.731777300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
